﻿using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Linq;

namespace DTcms.Core.API;

public class ThemeViewLocationExpander : IViewLocationExpander
{
    public const string ThemeKey = "Theme";
    public void PopulateValues(ViewLocationExpanderContext context)
    {
        string theme = context.ActionContext.HttpContext.Items[ThemeKey].ToString();
        context.Values[ThemeKey] = theme;
    }
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        string theme;

        if (context.Values.TryGetValue(ThemeKey, out theme))
        {
            viewLocations = new[]
            {
                $"/Views/{theme}/{{1}}/{{0}}.cshtml",
                $"/Views/{theme}/{{0}}.cshtml",
                $"/Views/{theme}/Shared/{{0}}.cshtml"
            }
            .Concat(viewLocations);
        }
        return viewLocations;
    }
}
